package cn.ekuma.epos.toolcommand;

import javax.swing.JComponent;
import javax.swing.JPanel;

import net.xeoh.plugins.base.PluginManager;
import net.xeoh.plugins.base.util.PluginManagerUtil;

import cn.ekuma.epos.module.ERP_UI_Swing_Model;
import cn.ekuma.rabbit.spi.I_ModelPart;

import com.openbravo.data.basic.BasicException;
import com.openbravo.pos.base.AppView;
import com.openbravo.pos.base.BeanFactoryApp;
import com.openbravo.pos.base.BeanFactoryException;
import com.openbravo.pos.base.I_AppView;
import com.openbravo.pos.forms.JPanelMenu;
import com.openbravo.pos.forms.JPanelView;
import com.openbravo.pos.forms.MenuDefinition;
import com.openbravo.pos.forms.MenuItemDefinition;
import com.openbravo.pos.forms.MenuTitleDefinition;

import java.awt.BorderLayout;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class JToolCommandPanel extends JPanel implements JPanelView, BeanFactoryApp,I_ModelPart<ERP_UI_Swing_Model>{

	JPanelMenu menu;
	MenuDefinition rootMenu;
	PluginManager pluginManager;
	Map<String,CommandGroup> groupMap;
	private boolean created = false;
	AppView app;
	/**
	 * Create the panel.
	 */
	public JToolCommandPanel() {
		initComponents();
	}
	
	private void initComponents() {
		setLayout(new BorderLayout(0, 0));
	}

	@Override
	public Object getBean() {
		return this;
	}

	@Override
	public void init(I_AppView app) throws BeanFactoryException {
		this.app=(AppView) app;
	}

	@Override
	public String getTitle() {
		return null;
	}

	@Override
	public void activate() throws BasicException {
		if (created) 
			return;	
		PluginManagerUtil pmu = new PluginManagerUtil(pluginManager);
        groupMap=new HashMap<String,CommandGroup>();
        rootMenu=new MenuDefinition("root");
        menu=new JPanelMenu(rootMenu);
        
        Collection<I_Command> plugins = pmu.getPlugins(I_Command.class);
        CommandGroup tempGroup=null;
        String categoryName;
        for(I_Command c:plugins){
        	c.init(app);
        	if(c instanceof I_ModelPart)
        		((I_ModelPart)c).setModel(mode);
        	categoryName=c.getCommandCategoryName()==null?"define":c.getCommandCategoryName();
        	tempGroup=groupMap.get(categoryName);
        	if(tempGroup==null){
        		tempGroup=new CommandGroup(categoryName);
        		groupMap.put(categoryName, tempGroup);
        	}
        	tempGroup.addCommand(c);	
        }
        
    	for(CommandGroup g:groupMap.values()){
    		new MenuTitleDefinition(g.getGroupName()).addComponent(menu);
    		for(I_Command c:g.getInCommand()){
    			new MenuItemDefinition(new CommandAction(c)).addComponent(menu);	
    		}
    	}

		add(menu,java.awt.BorderLayout.CENTER);
    	menu.getComponent().applyComponentOrientation(getComponentOrientation());
    	menu.activate();
    	created=true;
	}

	@Override
	public boolean deactivate() {
		return true;
	}

	@Override
	public JComponent getComponent() {
		return this;
	}

	@Override
	public String getModelName() {
		// TODO Auto-generated method stub
		return null;
	}
	
	private ERP_UI_Swing_Model mode;
	
	@Override
	public void setModel(ERP_UI_Swing_Model mode) {
		pluginManager=mode.getManager().getPluginManager();
		this.mode=mode;
	}

}
